package com.yunjian.admin.config;

import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint;
import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import com.google.common.base.Predicate;
import com.yunjian.admin.config.log.ChangeLogMvcEndpoint;
import com.yunjian.admin.controller.MainController;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Bean
	public Docket createRestApi() {
		Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
			@Override
			public boolean apply(RequestHandler input) {
				Class<?> declaringClass = input.declaringClass();
				if (declaringClass == BasicErrorController.class) { // 排除
					return false;
				}
				if (declaringClass == ChangeLogMvcEndpoint.class) { // 排除
					return false;
				}
				if (declaringClass == EndpointMvcAdapter.class) { // 排除
					return false;
				}
				if (declaringClass == HealthMvcEndpoint.class) { // 排除
					return false;
				}
				if (declaringClass == MetricsMvcEndpoint.class) { // 排除
					return false;
				}
				if (declaringClass == MetricsMvcEndpoint.class) { // 排除
					return false;
				}
				if (declaringClass == EnvironmentMvcEndpoint.class) { // 排除
					return false;
				}
				if (declaringClass == MainController.class) { // 排除
					return false;
				}
				/*if (declaringClass.isAnnotationPresent(RestController.class)) { // 被注解的类
					return true;
				}
				if (input.isAnnotatedWith(ResponseBody.class)) {// 被注解的方法
					return true;
				}*/
				return true;
			}
		};
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
				.useDefaultResponseMessages(false).select().apis(predicate)
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("和昌租赁管理平台API")// 大标题
				.version("1.0")// 版本
				.build();
	}
}